package com.message.util;

import java.util.concurrent.*;

public class ThreadPoolUtil {

    private static volatile ExecutorService executorService=null;
    private static volatile ScheduledExecutorService  scheduledThreadPoolExecutor=null;

    static {
        if (executorService == null && scheduledThreadPoolExecutor == null) {
            synchronized (ThreadPoolUtil.class) {
                if (executorService == null && scheduledThreadPoolExecutor == null) {//双重校验锁，防止多线程环境下出现初始化多个线程池的情况
                    int processors = Runtime.getRuntime().availableProcessors();
                    executorService = new ThreadPoolExecutor(processors, 1000,
                            10, TimeUnit.SECONDS,
                            new LinkedBlockingQueue<>());
                    scheduledThreadPoolExecutor = Executors.newSingleThreadScheduledExecutor();
                    scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> {
                            JedisPoolUtil.initUserTable(); //重新初始化映射表缓存
                    }, 24, 24, TimeUnit.HOURS);//每隔24h刷新一次缓存
                }
            }
        }
    }

    public static ExecutorService getExecutorService() {
        return executorService;
    }

    public static ScheduledExecutorService  getScheduledThreadPoolExecutor() {
        return scheduledThreadPoolExecutor;
    }

}
